﻿using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.Entities;

namespace TN_CMS.Domain.Concrete
{
    public class EFSysAdminRepository : BaseRepository, ISysAdminRepository
    {
        public EFSysAdminRepository()
        {
        }

        public List<SysAdmin> GetList(int pageIndex, int pageSize, out int total, string orderClause, string whereClause)
        {
            var map = MapBuilder<SysAdmin>
                .MapAllProperties()
                .DoNotMap(m => m.CreateAdmin)
                .DoNotMap(m => m.UpdateAdmin)
                .Build();
            var parameters = new DbParameter[] { 
                    this._db.CreateParameter("PageIndex", pageIndex, DbType.Int32),
                    this._db.CreateParameter("PageSize", pageSize, DbType.Int32),
                    this._db.CreateParameter("TotalCounts", 0, DbType.Int32, ParameterDirection.Output),
                    this._db.CreateParameter("OrderClause", orderClause, DbType.String),
                    this._db.CreateParameter("WhereClause", whereClause, DbType.String)
                };
            var lst = this._db.Execute("SP_SysAdmin_GetList", map, parameters).ToList();
            total = (int)parameters[2].Value;

            return lst;
        }

        public SysAdmin GetDetail(int id, bool getInactive = true)
        {
            using (var context = new EFDbContext())
            {
                var admin = context.SysAdmins.FirstOrDefault(a => a.Id == id && (a.IsActive == true || a.IsActive == !getInactive));

                //force EF load roles and features
                admin.SysRoles.Select(a => a);
                foreach (var f in admin.SysRoles)
                {
                    f.SysFeatures.Select(fe => fe);
                }

                return admin;
            }

        }

        public int Save(SysAdmin admin, int actorId)
        {
            bool needToSave = false;
            using (var context = new EFDbContext())
            {
                if (admin.Id == -1)
                {
                    admin.CreatedBy = actorId;
                    // admin.CreatedOn = DateTime.Now;
                    context.SysAdmins.Add(admin);
                    needToSave = true;
                }
                else
                {
                    SysAdmin dbEntry = context.SysAdmins.FirstOrDefault(a => a.Id == admin.Id);

                    if (dbEntry != null)
                    {
                        dbEntry.Name = admin.Name;
                        dbEntry.Email = admin.Email;

                        if (!string.IsNullOrEmpty(admin.Password))
                        {
                            dbEntry.Password = admin.Password;
                        }

                        dbEntry.UpdatedOn = DateTime.Now;
                        dbEntry.UpdatedBy = actorId;
                        dbEntry.IsActive = admin.IsActive;
                        needToSave = true;
                    }
                }

                if (needToSave)
                {
                    return context.SaveChanges();
                }
                else
                {
                    return 0;
                }
            }

        }

        public int UpdateActive(int[] arrId, bool isActive)
        {
            bool needToSave = false;

            foreach (var id in arrId)
            {
                SysAdmin dbEntry = GetDetail(id);

                if (dbEntry != null)
                {
                    dbEntry.IsActive = isActive;
                    needToSave = true;
                }
            }

            if (needToSave)
            {
                using (var context = new EFDbContext())
                {
                    return context.SaveChanges();
                }
            }
            else
            {
                return 0;
            }
        }

        public int Delete(int[] ids)
        {
            bool needSaveChange = false;
            using (var context = new EFDbContext())
            {
                foreach (var id in ids)
                {
                    SysAdmin dbEntry = GetDetail(id);
                    if (dbEntry != null)
                    {
                        context.SysAdmins.Remove(dbEntry);
                        needSaveChange = true;
                    }
                }

                if (needSaveChange)
                {
                    return context.SaveChanges();
                }
                else
                {
                    return 0;
                }
            }
        }

        public SysAdmin Login(string name, string hashedPass)
        {
            using (var context = new EFDbContext())
            {
                var admin = context.SysAdmins.FirstOrDefault(a => a.Name == name && a.Password == hashedPass && a.IsActive);

                if (admin != null)
                {
                    //force EF load roles and features
                    admin.SysRoles.Select(r => r);
                    foreach (var f in admin.SysRoles)
                    {
                        f.SysFeatures.Select(fe => fe);
                    }

                    admin.SysRoles.Select(a => a);
                }

                return admin;
            }
        }
    }
}
